$(function(){
	
	//初始化页面
	loadAllCustomers();
	/*定位到展示用户列表的坐标*/
	var customers_info_table = $("#customers_info_table");
	var customerAjaxOperator = new CustomerAjaxOperator();
	var customerInfoRenderer = new CustomerInfoRenderer();
	var dateFormatUtil = new DateFormatUtil(); //时间格式化工具

	//前台维护的customer对象，和后台一致
	function Customer(){
		let id;
		let name;
		let sex;
		let sal;
		let birth;
	}



	function CustomerAjaxOperator(){
		var obj = {};

		obj.loadAllCustomers = function loadAllCustomers(renderer){
			$.ajax({
				type:"POST",
				url:"./loadAllCustomers",
				dataType:"json"
			}).done(function(customers){
				renderer(customers);
			});
		}

		obj.addCustomer = function addCustomer(customerJson, renderer){
			$.ajax({
				type:"POST",
				url:"./addCustomer",
				data: customerJson,
				contentType: "application/json"
			}).done(function(customer){
				renderer(customer);
			});
		};

		obj.delCustomer = function delCustomer(customerId, renderer){
			$.ajax({
				type:"POST",
				url:"./delCustomer?customerId="+customerId
			}).done(function(){

			})
		};

		obj.updateOneCustomer = function updateOneCustomer(customer){

		};



		return obj;
	}


	/*渲染器*/
	function CustomerInfoRenderer(){
		let obj = {};
		obj.renderCustomerInfo = function(customer){
			let customerInfo = "<tr>"+
									"<td id='current_customer_id'>"+customer.id +"</td>"+
									"<td>"+customer.name+"</td>"+
									"<td>"+customer.sex +"</td>"+
									"<td>"+customer.sal +"</td>"+
									"<td>"+dateFormatUtil.formatDate(customer.birth)+"</td>"+
									"<td>"+"<button id='delete_one_customer'> 删除 </button>" +"</td>"+
									"<td>"+"<button id='update_one_customer'> 更新 </button>" +"</td>"+
								"</tr>";
			customers_info_table.append($.parseHTML(customerInfo));
		};

		obj.delCustomerInfo = function(item){
			item.remove();
		}




		return obj;
	}

	//时间格式化工具
	function DateFormatUtil(){
		var obj = {};

		obj.formatDate = function(time){
			let newDate = new Date();
			newDate.setTime(time);
			let fmtDate = newDate.getFullYear() +"年" + newDate.getMonth() +"月" +
						  newDate.getDate() +"日";
			return fmtDate;
		}
		return obj;
	}


	/*各种监听器*/
	$("#add_one_customer").on("click", function(){
		let customer = new Customer();
		customer.name = $("#name").val();
		customer.sex = $("input:radio:checked").val();
		customer.sal = $("#sal").val();
		customer.birth = $("#birth").val();
		customerAjaxOperator.addCustomer(JSON.stringify(customer), customerInfoRenderer.renderCustomerInfo);
	});

	//删除 table 的一条记录
	$("#customers_info_table").on("click", function(event){
		if($(event.target).attr("id") == "delete_one_customer"){
			let delBtn = $(event.target);
			let customerItem = delBtn.parent().parent();
			let customerId = customerItem.children(":first-child").text();
			customerItem.remove();
			customerAjaxOperator.delCustomer(customerId);
		}
	})

});